package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.bean.TimeRecomBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: TimeRecommendBannerAdapter
 * @Description: 时段推荐banner的adapter
 * @Author: ljp
 * @CreateDate: 2018/10/17 16:20
 * @Version: 1.0
 */
public class TimeRecommendBannerAdapter extends RecyclerView.Adapter<TimeRecommendBannerAdapter.ItemViewHolder> {
    private static  String TAG = "TimeRecommendBannerAdapter";
    private final Context context;
    private OnClick onClick;

    private List<TimeRecomBean.ListBean> lists;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(context,bitmap,view);
               /* GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(view);*/
            }
        }
    };

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public TimeRecommendBannerAdapter(Context context, List<TimeRecomBean.ListBean> urlList) {
        this.context = context;
        this.lists = urlList;

    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_time_recommend, viewGroup, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int position) {
        if(itemViewHolder instanceof ItemViewHolder){
            TimeRecomBean.ListBean listBean = lists.get(position);
            if (!listBean.getAvatarUrl().equals(itemViewHolder.imageIcon.getTag(R.id.image_icon))) {
                // 加载图片
                itemViewHolder.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+listBean.getAvatarUrl(),
                        itemViewHolder.imageIcon);
                //setImage(listBean.getAvatarUrl(), itemViewHolder.imageIcon);
            }
            setImage(listBean.getAvatarUrl(), itemViewHolder.imageIcon);
            itemViewHolder.tvNikName.setText(listBean.getNickName());
            itemViewHolder.tvContent.setText(listBean.getAutograph());
            itemViewHolder.tvParse.setText(listBean.getGold()+"金币/分");
           // Log.i(TAG, "onBindViewHolder: -------"+listBean.getVoiceInfo());
            if(!TextUtils.isEmpty(listBean.getVoiceInfo()) && listBean.getDuration() != 0){
                try{
                    //Log.i("onBindViewHolder", "onBindViewHolder: -----"+path);
                    //if(FileUtils.isExists(path)){
                    itemViewHolder.linearVoice.setVisibility(View.VISIBLE);
                    itemViewHolder.voiceTime.setText(listBean.getDuration()+"”");
                    //}
                }catch (RuntimeException e){

                }
            }else {
                itemViewHolder.linearVoice.setVisibility(View.GONE);
            }
            /*if(!TextUtils.isEmpty(listBean.getVoiceInfo())){
                itemViewHolder.linearVoice.setVisibility(View.VISIBLE);
            }else {
                itemViewHolder.linearVoice.setVisibility(View.GONE);
            }*/
            itemViewHolder.imageIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setIconClick(listBean);
                }
            });
            itemViewHolder.imageVoicePlay.setBackgroundResource(R.drawable.red_voice_play);
            AnimationDrawable frameAnimatio = (AnimationDrawable) itemViewHolder.imageVoicePlay.getBackground();
            itemViewHolder.linearVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setVoiceClick(position, itemViewHolder.imageVoicePlay,frameAnimatio,listBean);
                }
            });
            itemViewHolder.imageCallVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.setVoiceChatClick(listBean);
                }
            });
        }
    }
    /**
     * @return
     * @method
     * @description 获取头像
     * @date: 2018/10/19 17:21
     * @author: ljp
     */
    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                       // Log.i(TAG, "onDownFileSuccess: ----------"+inputStream);
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = view;
                        Bundle bundle = new Bundle();
                        //bundle.putString("tag",url);
                        bundle.putByteArray("bitmap", bitmap);
                        message.setData(bundle);
                        handler.sendMessage(message);
                        //  }
                    }

                    @Override
                    public void onDownFileProgress(String progress) {

                    }
                });
            }
        }).start();

    }
    @Override
    public int getItemCount() {
        return lists.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.tv_nik_name)
        TextView tvNikName;
        @BindView(R.id.voice_time)
        TextView voiceTime;
        @BindView(R.id.linear_voice)
        LinearLayout linearVoice;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.image_call_voice)
        ImageView imageCallVoice;
        @BindView(R.id.tv_parse)
        TextView tvParse;
        @BindView(R.id.image_voice_play)
        ImageView imageVoicePlay;
        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface OnClick{
        void setIconClick(TimeRecomBean.ListBean listBean);
        void setVoiceClick(int position,View view,AnimationDrawable frameAnimatio,TimeRecomBean.ListBean listBean);
        void setVoiceChatClick(TimeRecomBean.ListBean listBean);
    }
}
